package com.ywjh.inter.work;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.ywjh.inter.common.MemKey;
import com.ywjh.inter.conn.MemcacheConn;
import com.ywjh.inter.dao.DocBGluPlanDAO;
import com.ywjh.inter.dao.DocDiseaseDAO;
import com.ywjh.inter.dao.DocGroupDAO;
import com.ywjh.inter.dao.DocGroupDiseaseDAO;
import com.ywjh.inter.dao.DocHisDiseaseDAO;
import com.ywjh.inter.dao.DocMedTypeDAO;
import com.ywjh.inter.dao.DocMedicineDAO;
import com.ywjh.inter.entity.DocBGluPlanEntity;
import com.ywjh.inter.entity.DocDiseaseEntity;
import com.ywjh.inter.entity.DocGroupDiseaseEntity;
import com.ywjh.inter.entity.DocGroupEntity;
import com.ywjh.inter.entity.DocHisDiseaseEntity;
import com.ywjh.inter.entity.DocMedTypeEntity;
import com.ywjh.inter.entity.DocMedicineEntity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class DocWork {
	public void publish(Connection conn) throws SQLException {
		List<DocDiseaseEntity> dislist=new DocDiseaseDAO().list(conn);
		JSONArray disarray=new JSONArray();
		for(DocDiseaseEntity e:dislist) {
			JSONObject json=new  JSONObject();
			json.put("id", e.getId());
			json.put("name", e.getName());
			disarray.add(json);
		}
		System.out.println("disarray=="+disarray.size());
		MemcacheConn.getInstance().put(MemKey.disease, disarray.toString());
		
		List<DocHisDiseaseEntity> hisDislist=new DocHisDiseaseDAO().list(conn);
		JSONArray hisDisarray=new JSONArray();
		for(DocHisDiseaseEntity e:hisDislist) {
			JSONObject json=new  JSONObject();
			json.put("id", e.getId());
			json.put("name", e.getName());
			hisDisarray.add(json);
		}
		System.out.println("his_disease=="+hisDisarray.size());
		MemcacheConn.getInstance().put(MemKey.his_disease, hisDisarray.toString());
		
		JSONArray medarray=new JSONArray();
		List<DocMedTypeEntity> medTypeList=new DocMedTypeDAO().list(conn);
		for(DocMedTypeEntity medTypeEntity:medTypeList) {
			JSONObject medType=new JSONObject();
			medType.put("type_id", medTypeEntity.getId());
			medType.put("type_name", medTypeEntity.getName());
			JSONArray med_array=new JSONArray();
			List<DocMedicineEntity> medicinelist=new DocMedicineDAO().listByType(conn,medTypeEntity.getId());
			for(DocMedicineEntity e:medicinelist) {
				JSONObject json=new  JSONObject();
				json.put("id", e.getId());
				json.put("name", e.getName());
				json.put("unit", e.getUnit());
				json.put("usage", e.getDrugusage());
				json.put("type_id", e.getType_id());
				med_array.add(json);
			}
			medType.put("med_array", med_array);
			medarray.add(medType);
		}
		
		MemcacheConn.getInstance().put(MemKey.medicine, medarray.toString());
		
		List<DocBGluPlanEntity> planlist=new DocBGluPlanDAO().list(conn);
		JSONArray planarray=new JSONArray();
		for(DocBGluPlanEntity e:planlist) {
			JSONObject json=new  JSONObject();
			json.put("id", e.getId());
			json.put("name", e.getName());
			planarray.add(json);
		}
		System.out.println("medicine=="+medarray.size());
		MemcacheConn.getInstance().put(MemKey.bglu_plan, planarray.toString());
		
		List<DocGroupEntity> groupList=new DocGroupDAO().list(conn);
		JSONArray grouparray=new JSONArray();
		for(DocGroupEntity e:groupList) {
			JSONObject json=new  JSONObject();
			json.put("id", e.getId());
			json.put("name", e.getName());
			grouparray.add(json);
		}
		System.out.println("medicine=="+grouparray.size());
		MemcacheConn.getInstance().put(MemKey.group, grouparray.toString());
		
		
		List<DocGroupDiseaseEntity> groupDisList=new DocGroupDiseaseDAO().list(conn);
		JSONArray groupDisArray=new JSONArray();
		for(DocGroupDiseaseEntity e:groupDisList) {
			JSONObject json=new  JSONObject();
			json.put("id", e.getId());
			json.put("name", e.getName());
			json.put("group_id", e.getDoc_group_id());
			groupDisArray.add(json);
		}
		System.out.println("medicine=="+groupDisArray.size());
		MemcacheConn.getInstance().put(MemKey.group_disease, groupDisArray.toString());
	}
}
